package com.springboot.service.serviceImp;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.springboot.dao.loginDao;
import com.springboot.pojo.base.BaseModel;
import com.springboot.pojo.loginPojo;
import com.springboot.service.loginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;

@Service("loginServiceImp")
public class loginServiceImp implements loginService {
    @Autowired
    loginDao loginDao;

    @Override
    public String selectLoginInfo(String username, BaseModel baseModel)throws Exception{
        try {
            loginPojo login_user=loginDao.selectLoginInfo(username);//获得数据库中该条用户信息

            //生成时间戳
            Calendar calendar = Calendar.getInstance();
            Long timsStamp=calendar.getTimeInMillis();
            login_user.setTimestamp(timsStamp);

            //生成tooken
            String token= JWT.create().withAudience(login_user.getId()+"").sign(Algorithm.HMAC256(login_user.getPassword()));
            login_user.setToken(token);

            baseModel.setData(loginDao.selectLoginInfo(username));
            return loginDao.selectLoginInfo(username).getName();
        }catch (Exception e){
            return "1";
        }
//        baseModel.setData(loginDao.selectLoginInfo(username));

    }


}
